package com.anlogic.sdk.utils.loggers;

import org.eclipse.jface.text.DocumentEvent;
import org.eclipse.jface.text.IDocumentListener;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.console.TextConsole;
import org.eclipse.ui.console.TextConsoleViewer;

public class LoggerConsoleViewer extends TextConsoleViewer {
	private IDocumentListener documentListener;

	public LoggerConsoleViewer(Composite parent, TextConsole console) {
		super(parent, console);
		setup();
	}

	protected void handleDispose() {
		cleanup();
		super.handleDispose();
	}

	private void setup() {
		this.documentListener = new IDocumentListener() {
			public void documentChanged(DocumentEvent event) {
				LoggerConsoleViewer.this.revealEndOfDocument();
			}

			public void documentAboutToBeChanged(DocumentEvent event) {
			}
		};
		getDocument().addDocumentListener(this.documentListener);
	}

	private void cleanup() {
		getDocument().removeDocumentListener(this.documentListener);
		this.documentListener = null;
	}
}
